Completed
Push — master ( 9b9d7b...7811b9 )
by Leon
01:51
created

cmb2MetaBoxField.field_name_change   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
jQuery( function( $ ) {
2
3
	/**
4
	 * JS meta_box field actions.
5
	 */
6
	var cmb2MetaBoxField = {
7
8
		/**
9
		 * Initialize meta box fields behavior.
10
		 */
11
		init: function() {
12
            this.show_hide_on_load();
13
			this.field_type_change();
14
			this.field_name_change();
15
			this.display_usage_functions();
16
		},
17
18
		/**
19
		 * Show / hide field rows based on field_type_select selected value.
20
		 *
21
		 * show_hide
22
		 *
23
		 *
24
		 *
25
		 */
26
		show_hide: function( fieldTypeSelect ) {
27
            var fieldTypeVal = $( fieldTypeSelect ).val();
28
            var fieldSet     = $( fieldTypeSelect ).closest( '.cmb-field-list' );
29
            $( '.cmb-row.cmb_hide_field', fieldSet ).hide();
30
            $( '.cmb-row.' + fieldTypeVal, fieldSet ).show();
31
		},
32
33
		/**
34
		 * Use the show_hide() function on each field group's field rows on page load.
35
		 *
36
		 * show_hide_on_load
37
		 *
38
		 *
39
		 *
40
		 */
41
		show_hide_on_load: function() {
42
            var fieldTypeSelects = $( '.field_type_select' );
43
            fieldTypeSelects.each( function() {
44
                cmb2MetaBoxField.show_hide( this );
45
            } );
46
47
		},
48
49
		/**
50
		 * Use the show_hide() function on this field group's field rows on field_type_select change.
51
		 *
52
		 * field_type_change
53
		 *
54
		 *
55
		 *
56
		 */
57
		field_type_change: function() {
58
            $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) {
59
                var fieldTypeSelect = e.target;
60
                cmb2MetaBoxField.show_hide( fieldTypeSelect );
61
            } );
62
		},
63
64
		/**
65
		 * Update the usage code snippets when field_name changes.
66
		 *
67
		 * field_name_change
68
		 *
69
		 *
70
		 *
71
		 */
72
		field_name_change: function() {
73
            $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
74
                this.display_usage_functions();
75
            }.bind( this ) );
76
		},
77
78
		/**
79
		 * Convert field name to usage code snippet.
80
		 *
81
		 * display_usage_functions
82
		 *
83
		 *
84
		 *
85
		 */
86
		display_usage_functions: function() {
87
			var field_name = $( '.field_name' );
88
			var wrapper = field_name.closest( '.cmb-field-list' );
89
			var get_post_meta = $( '.get_post_meta', wrapper );
90
			var cmbf = $( '.cmbf', wrapper );
91
			get_post_meta.val( 'get_post_meta( get_the_ID(), \'_' + field_name.val().toLowerCase().replace( / /g, '_' ) + '\', true );');
92
			cmbf.val( 'cmbf( get_the_ID(), \'_' + field_name.val().toLowerCase().replace( / /g, '_' ) + '\' );');
93
		}
94
	};
95
96
	cmb2MetaBoxField.init();
97
98
} );
99